#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/config/SourcePrefix.h>
#include "udk.h"
#include "platform.h"
#include "udktest_api.h"

// Registers the fixture into the 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION( UDKApiTest );

static const char* vaid_tcp_connstring = "protocol=TCP,Address=192.168.0.125,port=4370,timeout=4000,passwd=";
static const char* vaid_tcp_connstring2 = "protocol=TCP,Address=192.168.0.126,port=4370,timeout=4000,passwd=";
static const char* vaid_udp_connstring = "protocol=UDP,Address=192.168.18.103,port=4370,timeout=4000,passwd=";

void 
UDKApiTest::testConnectUDP()
{
  UDK_HANDLE h;
  int prot=0;
  const char* connstring = "protocol=UDP,Address=255.255.255.255";
  h = UdkConnect(&prot,connstring);
  CPPUNIT_ASSERT(h==NULL);
  prot=1;
  h = UdkConnect(&prot,connstring);
  CPPUNIT_ASSERT(h==NULL);
  h = UdkConnect(&prot,connstring);
  prot=2;
  CPPUNIT_ASSERT(h==NULL);
}

void 
UDKApiTest::testUdkSearchDevicesUDP()
{
	int bufsize;
	int ret;
	char* searchbuf = (char*)udk_malloc(128*1024);
	bufsize = 128*1024;
	ret = UdkSearchDevices((char*)"UDP",(char*)"255.255.255.255",searchbuf,&bufsize);
	CPPUNIT_ASSERT(ret>=0);
	if(ret>0) {
		CPPUNIT_ASSERT(bufsize!=128*1024);
		UDK_LOGI("found %d devices\n",ret);
		UDK_DUMP("search devices buffer",searchbuf,bufsize);
	}
	bufsize = 128*1024;
	ret = UdkSearchDevices((char*)"UDP",(char*)"255.0.0.0",searchbuf,&bufsize);
	CPPUNIT_ASSERT(ret>=0);
	if(ret>0) {
		CPPUNIT_ASSERT(bufsize!=128*1024);
		UDK_LOGI("found %d devices\n",ret);
	}
	bufsize = 128*1024;
	ret = UdkSearchDevices((char*)"UDP",(char*)"255.255.0.0",searchbuf,&bufsize);
	CPPUNIT_ASSERT(ret>=0);
	if(ret>0) {
		CPPUNIT_ASSERT(bufsize!=128*1024);
		UDK_LOGI("found %d devices\n",ret);
	}
	bufsize = 128*1024;
	ret = UdkSearchDevices((char*)"UDP",(char*)"224.0.0.0",searchbuf,&bufsize);
	CPPUNIT_ASSERT(ret>=0);	
	if(ret>0) {
		CPPUNIT_ASSERT(bufsize!=128*1024);
		UDK_LOGI("found %d devices\n",ret);
	}
	bufsize = 128*1024;
	ret = UdkSearchDevices((char*)"UDP",(char*)"192.168.0.255",searchbuf,&bufsize);
	CPPUNIT_ASSERT(ret>=0);
	if(ret>0) {
		CPPUNIT_ASSERT(bufsize!=128*1024);
		UDK_LOGI("found %d devices\n",ret);
		UDK_DUMP("search devices buffer",searchbuf,bufsize);
	}
	udk_free(searchbuf);
}
void UDKApiTest::testUdkGetLockCountUDP()
{
	int prot=0;
	int ret; 
	UDK_HANDLE h;
	h = UdkConnect(&prot,vaid_udp_connstring);
	CPPUNIT_ASSERT(h!=NULL);	
	ret = UdkGetLockCount(h);
	CPPUNIT_ASSERT(ret>=0);
	UdkDisconnect(h);
}
void UDKApiTest::testUdkGetLockStateUDP()
{
	int prot=0;
	int ret;
	UDK_HANDLE h;
	h = UdkConnect(&prot,vaid_udp_connstring);
	CPPUNIT_ASSERT(h!=NULL);	
	ret = UdkGetLockState(h,0);
	UDK_LOGD("Lock State =%08x\n",ret);
	CPPUNIT_ASSERT(ret>=0);
	ret = UdkGetLockState(h,1);
	UDK_LOGD("Lock 1 State =%d\n",ret);
	CPPUNIT_ASSERT(ret>=0);
	ret = UdkGetLockState(h,2);
	UDK_LOGD("Lock 2 State =%d\n",ret);
	CPPUNIT_ASSERT(ret>=0);
	ret = UdkGetLockState(h,3);
	UDK_LOGD("Lock 3 State =%d\n",ret);
	CPPUNIT_ASSERT(ret>=0);
	ret = UdkGetLockState(h,4);
	UDK_LOGD("Lock 4 State =%d\n",ret);
	CPPUNIT_ASSERT(ret>=0);
	UdkDisconnect(h);
}
void UDKApiTest::testUdkSetLockStateUDP()
{
	int prot=0;
	UDK_HANDLE h = UdkConnect(&prot,vaid_udp_connstring);
	if(!h){
		CPPUNIT_FAIL("failed to connect\n");
	}else{
		CPPUNIT_ASSERT(UdkSetLockState(h,1,2)>=0);
		CPPUNIT_ASSERT(UdkSetLockState(h,1,0)>=0);
		CPPUNIT_ASSERT(UdkSetLockState(h,1,255)>=0);
		CPPUNIT_ASSERT(UdkSetLockState(h,1,100)>=0);
		CPPUNIT_ASSERT(UdkSetLockState(h,1,59)>=0);
		CPPUNIT_ASSERT(UdkSetLockState(h,1,60)>=0);
		UdkDisconnect(h);
	}

}
void 
UDKApiTest::testConnectTCP()
{
  UDK_HANDLE h;
  int prot=0;
  const char* connstring = "protocol=TCP,Address=192.168.1.1";
  h = UdkConnect(&prot,connstring);
  CPPUNIT_ASSERT(h==NULL);
  prot=1;
  h = UdkConnect(&prot,connstring);
  CPPUNIT_ASSERT(h==NULL);
  h = UdkConnect(&prot,connstring);
  prot=2;
  CPPUNIT_ASSERT(h==NULL);
  prot=0;
  h = UdkConnect(&prot,vaid_tcp_connstring);
  CPPUNIT_ASSERT(h!=NULL);
  UdkDisconnect(h);
 
}

void UDKApiTest::testUdkGetLockCountTCP()
{
	int prot=0;
	int ret; 
	UDK_HANDLE h;
	h = UdkConnect(&prot,vaid_tcp_connstring);
	CPPUNIT_ASSERT(h!=NULL);	
	ret = UdkGetLockCount(h);
	CPPUNIT_ASSERT(ret>=0);
	UdkDisconnect(h);
	prot=0;	
	h = UdkConnect(&prot,vaid_tcp_connstring2);
	CPPUNIT_ASSERT(h!=NULL);
	ret = UdkGetLockCount(h);
	CPPUNIT_ASSERT(ret>=0);
	UdkDisconnect(h);

}
void UDKApiTest::testUdkGetLockStateTCP()
{
	int prot=0;
	int ret;
	UDK_HANDLE h;
	h = UdkConnect(&prot,vaid_tcp_connstring);
	CPPUNIT_ASSERT(h!=NULL);	
	ret = UdkGetLockState(h,0);
	UDK_LOGD("Lock State =%08x\n",ret);
	CPPUNIT_ASSERT(ret>=0);
	ret = UdkGetLockState(h,1);
	UDK_LOGD("Lock 1 State =%d\n",ret);
	CPPUNIT_ASSERT(ret>=0);
	ret = UdkGetLockState(h,2);
	UDK_LOGD("Lock 2 State =%d\n",ret);
	CPPUNIT_ASSERT(ret>=0);
	ret = UdkGetLockState(h,3);
	UDK_LOGD("Lock 3 State =%d\n",ret);
	CPPUNIT_ASSERT(ret>=0);
	ret = UdkGetLockState(h,4);
	UDK_LOGD("Lock 4 State =%d\n",ret);
	CPPUNIT_ASSERT(ret>=0);
	UdkDisconnect(h);
	//test another connected device
	prot=0;
	h = UdkConnect(&prot,vaid_tcp_connstring2);
	CPPUNIT_ASSERT(h!=NULL);	
	ret = UdkGetLockState(h,0);
	UDK_LOGD("Lock State =%08x\n",ret);
	CPPUNIT_ASSERT(ret>=0);
	ret = UdkGetLockState(h,1);
	UDK_LOGD("Lock 1 State =%d\n",ret);
	CPPUNIT_ASSERT(ret>=0);
	ret = UdkGetLockState(h,2);
	UDK_LOGD("Lock 2 State =%d\n",ret);
	CPPUNIT_ASSERT(ret>=0);
	ret = UdkGetLockState(h,3);
	UDK_LOGD("Lock 3 State =%d\n",ret);
	CPPUNIT_ASSERT(ret>=0);
	ret = UdkGetLockState(h,4);
	UDK_LOGD("Lock 4 State =%d\n",ret);
	CPPUNIT_ASSERT(ret>=0);
	UdkDisconnect(h);
}
void UDKApiTest::testUdkSetLockStateTCP()
{
	int prot=0;
	UDK_HANDLE h = UdkConnect(&prot,vaid_tcp_connstring);
	if(!h){
		CPPUNIT_FAIL("failed to connect\n");
	}else{
		CPPUNIT_ASSERT(UdkSetLockState(h,1,2)>=0);
		CPPUNIT_ASSERT(UdkSetLockState(h,1,0)>=0);
		CPPUNIT_ASSERT(UdkSetLockState(h,1,255)>=0);
		CPPUNIT_ASSERT(UdkSetLockState(h,1,100)>=0);
		CPPUNIT_ASSERT(UdkSetLockState(h,1,59)>=0);
		CPPUNIT_ASSERT(UdkSetLockState(h,1,60)>=0);
		UdkDisconnect(h);
	}
	prot=0;
	h = UdkConnect(&prot,vaid_tcp_connstring);
	if(!h){
			CPPUNIT_FAIL("failed to connect\n");
	}else {
		CPPUNIT_ASSERT(UdkSetLockState(h,1,2)>=0);
		CPPUNIT_ASSERT(UdkSetLockState(h,1,0)>=0);
		CPPUNIT_ASSERT(UdkSetLockState(h,1,255)>=0);
		CPPUNIT_ASSERT(UdkSetLockState(h,1,100)>=0);
		CPPUNIT_ASSERT(UdkSetLockState(h,1,59)>=0);
		CPPUNIT_ASSERT(UdkSetLockState(h,1,60)>=0);
		UdkDisconnect(h);
	}

}
void UDKApiTest::testUdkSearchDevicesTCP()
{
	//currently TCP only support multicast,but sdk does not support it
}

void UDKApiTest::testConnectP4P(){

  UDK_HANDLE h;
  int prot=UDK_PROT_DEFAULT;
  h = UdkConnect(&prot,"protocol=P4P,Address=KA8FR278AVPRFFGA111A");
  CPPUNIT_ASSERT_MESSAGE("connect to \"protocol=P4P,Address=KA8FR278AVPRFFGA111A\" with prot=0",
	h==NULL);
  prot=UDK_PROT_PULL;
  CPPUNIT_ASSERT_MESSAGE("connect to \"protocol=P4P,Address=KA8FR278AVPRFFGA111A\" with prot=1",
	h==NULL);
  prot=UDK_PROT_STANDALONE;
  CPPUNIT_ASSERT_MESSAGE("connect to \"protocol=P4P,Address=KA8FR278AVPRFFGA111A\" with prot=1",
	h==NULL);
  
}



